package me.helllp.dake.iam.core.dao.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.helllp.dake.core.IdGenerator;
import me.helllp.dake.core.bean.DakeAuthAccount;
import me.helllp.dake.core.utils.PoUtil;
import me.helllp.dake.iam.core.bo.UserBo;
import me.helllp.dake.iam.core.convert.UserConvert;
import me.helllp.dake.iam.core.dao.IDakeUserService;
import me.helllp.dake.iam.core.mapper.DakeUserMapper;
import me.helllp.dake.iam.core.po.DakeUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author dake
 * @since 2022-12-05
 */
@Service
public class DakeUserServiceImpl extends ServiceImpl<DakeUserMapper, DakeUser> implements IDakeUserService {
    @Autowired
    private UserConvert userConvert;

    @Autowired
    private IdGenerator idGenerator;

    public Long creatUser(UserBo userBo, DakeAuthAccount authUser) {
        DakeUser user = this.userConvert.toUser(userBo);
        user.setId(this.idGenerator.nextId());
        PoUtil.setInsertPo(user, authUser.getUserAccount());
        return save(user) ? user.getId() : null;
    }

    public Boolean modifyUser(UserBo userBo, DakeAuthAccount authUser)
    {
        DakeUser user = this.userConvert.toUser(userBo);
        PoUtil.setUpdatePo(user, authUser.getUserAccount());
        return Boolean.valueOf(saveOrUpdate(user));
    }

    public Boolean deleteUser(Long userId)
    {
        return Boolean.valueOf(removeById(userId));
    }
}
